
package com.kugou.game.sdk.ui.widget;

import static android.view.View.MeasureSpec.AT_MOST;

import android.content.Context;
import android.util.AttributeSet;
import android.view.WindowManager;
import android.widget.RelativeLayout;

/**
 * 用于自动适配屏幕和layout的大小
 * 
 * @author liaodongmingRabbit
 */
public class GameFitGridRelativeLayout extends RelativeLayout {

    final static private int mColumn = 1;

    /**
     * 宽高比
     */
    private float mScale;

    private int mScreenWidth;

    private int mScreenPadding;

    public int getmScreenPadding() {
        return mScreenPadding;
    }

    public void setmScreenPadding(int mScreenPadding) {
        this.mScreenPadding = mScreenPadding;
    }

    public float getmScale() {
        return mScale;
    }

    public void setmScale(float mScale) {
        this.mScale = mScale;
    }

    public GameFitGridRelativeLayout(Context context) {
        this(context, null);
    }

    public GameFitGridRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GameFitGridRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        // final DisplayMetrics metrics =
        // getContext().getResources().getDisplayMetrics();
        // final int screenWidth = metrics.widthPixels;
        // final int screenHeight = metrics.heightPixels;
        final WindowManager wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        final int screenWidth = wm.getDefaultDisplay().getWidth();
        final int screenHeight = wm.getDefaultDisplay().getHeight();
        // final boolean isPortrait = screenWidth < screenHeight;
        // if (isPortrait) {
        mScreenWidth = screenWidth;
        // } else {
        // mScreenWidth = screenHeight;
        // }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                ((int) ((float) mScreenWidth / mColumn / mScale)), AT_MOST);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }
}
